package com.zkingsoft.rhi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelics;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsNature;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsPublication;
import com.zkingsoft.rhi.entity.ImmovableHistoricalRelicsType;
import com.zkingsoft.rhi.handler.Result;
import com.zkingsoft.rhi.mapper.ImmovableHistoricalRelicsMapper;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsNatureService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsPublicationService;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zkingsoft.rhi.service.ImmovableHistoricalRelicsTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 不可移动文物表 服务实现类
 * </p>
 *
 * @author xieguo
 * @since 2020-11-04
 */
@Service
public class ImmovableHistoricalRelicsServiceImpl extends ServiceImpl<ImmovableHistoricalRelicsMapper, ImmovableHistoricalRelics> implements ImmovableHistoricalRelicsService {

    @Autowired
    ImmovableHistoricalRelicsNatureService ihrns;

    @Autowired
    ImmovableHistoricalRelicsTypeService ihrts;

    @Autowired
    ImmovableHistoricalRelicsPublicationService ihrps;

    @Override
    public JSONObject queryImmovableRelics(JSONObject ihr) {
        Integer offset = (Integer) ihr.get("offset");
        Integer limit = (Integer) ihr.get("limit");
        String name = (String) ihr.get("name");
        Integer type = ihr.getInteger("type");
        Integer nature = ihr.getInteger("nature");
        String level = ihr.getString("level");
        String area = ihr.getString("area");
        Integer publicationId =ihr.getInteger("publicationNumber");
        String sqlType="";
        String sqlNature="";
        String publicationNumber="";
        if (null!=type&&type>0){
            ImmovableHistoricalRelicsType ihrt = ihrts.getById(type);
            sqlType= ihrt.getType();
        }

        if (null!=nature&&nature>0){
            ImmovableHistoricalRelicsNature ihrn = ihrns.getById(nature);
            sqlNature= ihrn.getNature();
        }

        if (null!=publicationId&&publicationId>0){
            ImmovableHistoricalRelicsPublication ihrp = ihrps.getById(publicationId);
            publicationNumber=ihrp.getPublicationNumber();
        }

        if (offset != null && limit != null && offset > 0 && limit > 0) {
            Page<ImmovableHistoricalRelics> ihrPage = new Page<>(offset, limit);
            QueryWrapper<ImmovableHistoricalRelics> ihrqw = new QueryWrapper<>();
            if (StringUtils.isNotBlank(name)) {
                ihrqw.like("name", name);
            }
            if (StringUtils.isNotBlank(sqlType)){
                ihrqw.eq("type",sqlType);
            }
            if (StringUtils.isNotBlank(sqlNature)){
                ihrqw.eq("nature",sqlNature);
            }
            if (StringUtils.isNotBlank(level)){
                ihrqw.like("level",level);
            }
            if (StringUtils.isNotBlank(publicationNumber)){
                ihrqw.like("publication_number",publicationNumber);
            }
            if (StringUtils.isNotBlank(area)){
                ihrqw.eq("area",area);
            }
            IPage<ImmovableHistoricalRelics> page = page(ihrPage,ihrqw);
            Integer total = (int) page.getTotal();
            return Result.setResultByCodeAndMsgAndDate(Result.STATUS_SUCCESS, "查询成功", page.getRecords(), total);
        } else {
            return Result.setResultByCodeAndMsg(Result.STATUS_FAIL, "参数错误");
        }
    }
}
